package com.keyword.utils;

import java.lang.reflect.InvocationTargetException;

/**
 * @author yhx
 */
public class Exceptions {
    public Exceptions() {
    }

    public static RuntimeException unchecked(Throwable e){
        if (e instanceof Error) {
            throw (Error)e;
        } else if (!(e instanceof IllegalAccessException) && !(e instanceof IllegalArgumentException) && !(e instanceof NoSuchMethodException)) {
            if (e instanceof InvocationTargetException) {
                return new RuntimeException(((InvocationTargetException)e).getTargetException());
            } else if (e instanceof RuntimeException) {
                return (RuntimeException)e;
            } else {
                if (e instanceof InterruptedException) {
                    Thread.currentThread().interrupt();
                }

                return (RuntimeException)runtime(e);
            }
        } else {
            return new IllegalArgumentException(e);
        }
    }

    private static <T extends Throwable> T runtime(Throwable throwable) throws T {
        throw throwable;
    }
}
